package cn.iocoder.yudao.module.system.service.userdetail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.userdetail.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userdetail.UserDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.system.convert.userdetail.UserDetailConvert;
import cn.iocoder.yudao.module.system.dal.mysql.userdetail.UserDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 艾灸师详情 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class UserDetailServiceImpl implements UserDetailService {

    @Resource
    private UserDetailMapper userDetailMapper;

    @Override
    public Long createUserDetail(UserDetailCreateReqVO createReqVO) {
        // 插入
        UserDetailDO userDetail = UserDetailConvert.INSTANCE.convert(createReqVO);
        userDetailMapper.insert(userDetail);
        // 返回
        return userDetail.getId();
    }

    @Override
    public void updateUserDetail(UserDetailUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserDetailExists(updateReqVO.getId());
        // 更新
        UserDetailDO updateObj = UserDetailConvert.INSTANCE.convert(updateReqVO);
        userDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserDetail(Long id) {
        // 校验存在
        validateUserDetailExists(id);
        // 删除
        userDetailMapper.deleteById(id);
    }

    private void validateUserDetailExists(Long id) {
        if (userDetailMapper.selectById(id) == null) {
            throw exception(USER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public UserDetailDO getUserDetail(Long id) {
        return userDetailMapper.selectById(id);
    }

    @Override
    public UserDetailDO getUserDetailByMobile(String mobile){
        return userDetailMapper.selectOne(UserDetailDO::getMobile, mobile);
    }


    @Override
    public List<UserDetailDO> getUserDetailByMobiles(Collection<String> mobiles){
        return userDetailMapper.selectList(UserDetailDO::getMobile, mobiles);
    }

    @Override
    public List<UserDetailDO> getUserDetailList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return userDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserDetailDO> getUserDetailPage(UserDetailPageReqVO pageReqVO) {
        return userDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserDetailDO> getUserDetailList(UserDetailExportReqVO exportReqVO) {
        return userDetailMapper.selectList(exportReqVO);
    }

}
